#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
#include <stdio.h>

#include <signal.h>
#include <sys/time.h>
#include <unistd.h>


void handler_func ( int sig );

int main ( int argc, const char * argv[] ) {
    struct sigaction myhandler;
    myhandler.sa_handler = &handler_func;
    sigemptyset(&myhandler.sa_mask);
    myhandler.sa_flags = 0;

//    sigaction(SIGALRM, &myhandler, NULL);

    struct timeval start;
    struct timeval interval;

    start.tv_sec = 1;
    interval.tv_sec = 1;

    struct itimerval timer;
    timer.it_value = start;
    timer.it_interval = interval;

    setitimer(ITIMER_REAL, &timer, NULL);

    for ( ;; )
        ;

    return 0;
}

void handler_func ( int sig ) {
    printf("Handling...\n");
}


//static void sig_quit ( int );
//
//int main ( void ) {
//    sigset_t    newmask, oldmask, pendmask;
//
//    if (signal(SIGQUIT, sig_quit) == SIG_ERR)
//        fprintf(stderr, "can't catch SIGQUIT\n");
//
//    /*
//     * Block SIGQUIT and save current signal mask.
//     */
//    sigemptyset(&newmask);
//    sigaddset(&newmask, SIGQUIT);
//    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
//        fprintf(stderr, "SIG_BLOCK error");
//
//    sleep(5);   /* SIGQUIT here will remain pending */
//    if (sigpending(&pendmask) < 0)
//        fprintf(stderr, "sigpending error");
//    if (sigismember(&pendmask, SIGQUIT))
//        printf("\nSIGQUIT pending\n");
//
//    /*
//     * Reset signal mask which unblocks SIGQUIT.
//     */
//    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
//        fprintf(stderr, "SIG_SETMASK error");
//    printf("SIGQUIT unblocked\n");
//
//    sleep(5);   /* SIGQUIT here will terminate with core file */
//    exit(0);
//}
//
//static void sig_quit ( int signo ) {
//    printf("caught SIGQUIT\n");
//    if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
//        fprintf(stderr, "can't reset SIGQUIT");
//}
//
/////* Prototypes. */
//void handlerFunction ( int sig );
//
//int main ( int argc, const char * argv[] ) {
//    /* My own timer test. */
//    printf("Welcome.\n");
//
//
//    /* Create myHandler (sigaction struct) needed to modify responses to signals. */
//    struct sigaction myHandler;
//
//    /* Member sa_handler holds the pointer to the handling function for some signal.
//     * Function must be: void function ( int signal ); */
//    myHandler.sa_handler = handlerFunction;
//
//    /* Initialize the set of signals contained in member sa_mask to be empty. */
//    sigemptyset(&myHandler.sa_mask);
//
//    /* Set member sa_flags to 0 (review this). */
//    myHandler.sa_flags = 0;
//
//    /* Finally, a call to sigaction() will take the signal we want to customize the
//     * response to as first arg, the pointer to the *sigaction struct myHandler */
//    sigaction(SIGINT, &myHandler, 0);
//
//    while ( 1 ) {
//        printf("Hello World!\n");
//        sleep(1);
//    }
//
//    return 0;
//}
//
//void handlerFunction ( int sig ) {
//    printf("\tnope\n");
//    return;
//}
